<template>
  <view>
    <view v-for="(item,i) in goodsList" :key="i" @click="jumpDetail(item.goods_id)">
      <my-goods :goods="item"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        total: 0,
        goodsList: [],
        isLoading: false
      }
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // console.log(options)
      this.getGoodsList()
    },
    // 上拉加载
    onReachBottom() {
      if (this.isLoading) return
      if (this.goodsList.length >= this.total) return uni.$showToast()
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    // 下拉刷新
    onPullDownRefresh() {
      this.queryObj.pagenum=1
      this.total=0
      this.isLoading=false
      this.goodsList=[]
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    },
    methods: {
      async getGoodsList() {
        this.isLoading = true
        const {
          data: res
        } = await uni.$http.get('/goods/search', this.queryObj)
        this.isLoading = false
        if (res.meta.status !== 200) return uni.$showMsg()
        this.total = res.message.total
        this.goodsList = [...this.goodsList, ...res.message.goods]
        // console.log(this.goodsList)
      },
      // 跳转详情页面
      jumpDetail(id){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+id
        })
      }
    }
  }
</script>

<style lang="scss">
</style>
